﻿using ShopManagment.Components.Pages;
using ShopManagment.Models;
using ShopManagment.Service.IService;

namespace ShopManagment.Service;

public class ProductService : IProductService
{
    private List<Product> _products;
    public ProductService()
    {
        _products = new List<Product>()
        {
            new Product(){ProductId = 111,ProductDescription = "设计模式-001",ProductName = "设计模式"},
            new Product(){ProductId = 222,ProductDescription = "Dotnet-001",ProductName = "dotnet"},
            new Product(){ProductId = 333,ProductDescription = "Python-001",ProductName = "Python"},
            new Product(){ProductId = 444,ProductDescription = "Java-001",ProductName = "Java"}
        };
    }

    public void Add(Product product)
    {
        _products.Add(new Product() { ProductId = product.ProductId, ProductName = product.ProductName, ProductDescription = product.ProductDescription });
    }

    public void Update(Product product)
    {
        var item = _products.FirstOrDefault(x => x.ProductId == product.ProductId);

        if (item != null)
        {
            _products.Remove(item);

        }

        _products.Add(product);


    }

    public void Delete(long id)
    {
        var item = _products.FirstOrDefault(x => x.ProductId == id);

        if (item != null)
        {
            _products.Remove(item);

        }
    }

    public IEnumerable<Product> GetAll()
    {
        return _products;
    }

    public Product GetById(long? id)
    {
        if (id.HasValue)
        {
            var product = _products.FirstOrDefault(x => x.ProductId == id) ?? throw new ArgumentNullException("Not Found)");

            return product;
        }

        return new Product();

    }
}